package com.snow.useviewdemo.function.calendar;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.snow.useviewdemo.R;
import com.snow.useviewdemo.util.OnAntiDoubleClickListener;
import com.snow.useviewdemo.util.TextViewUtils;

import java.util.List;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-11-24
 * desc   :
 */
public class AdapterCalendarItem extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<CalendarData> dataList;
    private int itemHeight;
    private int itemWidth;
    private int onlySelectWH;
    CalendarItemClickListener itemClickListener;
    //当前点击日期 2019-11-02
    private String currentClickDate;
    //当前日期 2019-11-02
    private String todayDate;


    public AdapterCalendarItem(Context mContext, CalendarItemClickListener itemClickListener) {
        this.mContext = mContext;
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_calendar_adapter, null);
        return new ViewHolder(view);
    }

    public void setDataList(List<CalendarData> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    /**
     * 设置今天日期
     *
     * @param todayPosition
     */
    public void setTodayPosition(String todayPosition) {
        this.todayDate = todayPosition;
    }

    /**
     * 设置item的高度  总行数
     *
     * @param itemHeight
     * @param rowNum
     */
    public void setItemWidthHeight(int itemHeight, int rowNum, int itemWidth) {
        this.itemHeight = itemHeight;
        this.itemWidth = itemWidth;
        onlySelectWH = (int) (itemHeight - TextViewUtils.init().dp2px(mContext, 12) * (5 / 6f));
        if (onlySelectWH > itemWidth) {
            onlySelectWH = itemWidth;
        }
//        Log.e("snow_m", "====itemHeight=====" + itemHeight);
//        Log.e("snow_m", "====itemWidth=====" + itemWidth);
    }


    public void setCurrentClickDate(String clickDate) {
        this.currentClickDate = clickDate;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CalendarData data = dataList.get(position);
        ViewHolder vh = (ViewHolder) holder;
        //设置item的高
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) vh.root_view.getLayoutParams();
        layoutParams.height = itemHeight;
        vh.root_view.setLayoutParams(layoutParams);
        //设置选中的宽高
        RelativeLayout.LayoutParams onlySelectViewParams = (RelativeLayout.LayoutParams) vh.onlySelectView.getLayoutParams();
        onlySelectViewParams.height = onlySelectWH;
        onlySelectViewParams.width = onlySelectWH;
        vh.onlySelectView.setLayoutParams(onlySelectViewParams);

        vh.tv_num.setText(data.getSolarNum());
        //设置选中的===已签到状态
        if (data.getClockInBean() != null) {
            vh.tv_num.setTextColor(mContext.getResources().getColor(R.color.tt_FF8820));
            setSelectedView(vh.selectedView, position);
            vh.ivRemind.setVisibility(View.VISIBLE);
        } else {
            vh.tv_num.setTextColor(mContext.getResources().getColor(R.color.tt_33333));
            vh.ivRemind.setVisibility(View.INVISIBLE);
            vh.selectedView.setBackground(null);
        }
        //设置当前点击的效果
        if (!TextUtils.isEmpty(currentClickDate) && currentClickDate.equals(data.getYearMD())) {
            vh.ivRemind.setColorFilter(mContext.getResources().getColor(R.color.tt_white));
            vh.onlySelectView.setBackgroundResource(R.drawable.bg_ffbf01_circle);
            vh.tv_num.setTextColor(mContext.getResources().getColor(R.color.tt_white));
        } else {
            vh.ivRemind.setColorFilter(mContext.getResources().getColor(R.color.tt_FF8820));
//            vh.ivRemind.setColorFilter(mContext.getResources().getColor(R.color.tt_FFBF01));
            vh.onlySelectView.setBackground(null);
        }
        //设置今天对应的效果
        if (!TextUtils.isEmpty(data.getYearMD()) && data.getYearMD().equals(todayDate)) {
            vh.ivRemind.setColorFilter(mContext.getResources().getColor(R.color.tt_white));
            vh.onlySelectView.setBackgroundResource(R.drawable.bg_ff8820_circle);
            vh.tv_num.setTextColor(mContext.getResources().getColor(R.color.tt_white));
        }
        //item点击
        vh.root_view.setOnClickListener(new OnAntiDoubleClickListener() {
            @Override
            public void onAntiDoubleClick(View v) {
                if (itemClickListener != null) {
                    itemClickListener.itemClick(data, position);
                }
            }
        });
    }

    /**
     * 根据情况显示已选中（打卡）的样式
     *
     * @param selectedView
     * @param position
     */
    private void setSelectedView(View selectedView, int position) {
        if (position % 7 == 0) {//最左边的数字
            drawSelectView(selectedView, nextIsSelected(position, dataList) ? 3 : 0);
        } else if (position % 7 == 6) {//最右边的数字
            drawSelectView(selectedView, lastIsSelected(position, dataList) ? 2 : 0);
        } else {//中间数据
            boolean left = lastIsSelected(position, dataList);
            boolean right = nextIsSelected(position, dataList);
            if (left && right) {
                drawSelectView(selectedView, 1);
            } else if (left) {
                drawSelectView(selectedView, 2);
            } else if (right) {
                drawSelectView(selectedView, 3);
            } else {
                drawSelectView(selectedView, 0);
            }
        }
    }

    /**
     * 绘制选中的样式
     *
     * @param selectedView
     * @param flag
     */
    private void drawSelectView(View selectedView, int flag) {
        //设置选中的宽高
        RelativeLayout.LayoutParams onlySelectViewParams = (RelativeLayout.LayoutParams) selectedView.getLayoutParams();
        onlySelectViewParams.height = onlySelectWH;
        if (flag == 1) {//画全整个矩形
            selectedView.setBackgroundResource(R.color.tt_ffe2c9);
            onlySelectViewParams.width = itemWidth + 5;
        } else if (flag == 2) { //左矩形 右圆==靠左
            selectedView.setBackgroundResource(R.drawable.bg_ffe2c9_right_circle);
            onlySelectViewParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            onlySelectViewParams.width = itemWidth / 2 + onlySelectWH / 2;
        } else if (flag == 3) {//右矩形 左圆====靠右
            selectedView.setBackgroundResource(R.drawable.bg_ffe2c9_left_circle);
            onlySelectViewParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            onlySelectViewParams.width = itemWidth / 2 + onlySelectWH / 2;
        } else { //圆形
            selectedView.setBackgroundResource(R.drawable.bg_ffe2c9_circle);
            onlySelectViewParams.width = onlySelectWH;
            onlySelectViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        }
        selectedView.setLayoutParams(onlySelectViewParams);
    }

    /**
     * 上一条数据是否被选中
     *
     * @param position
     * @return
     */
    private boolean lastIsSelected(int position, List<CalendarData> dataList) {
        if (dataList == null) {
            return false;
        }
        CalendarData lastData = null;
        if (position > 0) {
            lastData = dataList.get(position - 1);
        }
        if (lastData == null || lastData.getClockInBean() == null) {
            return false;
        }
        return true;
    }

    /**
     * 下一条数据是否被选中
     *
     * @param position
     * @return
     */
    private boolean nextIsSelected(int position, List<CalendarData> dataList) {
        if (dataList == null) {
            return false;
        }
        CalendarData nextData = null;
        if (position < dataList.size() - 1) {
            nextData = dataList.get(position + 1);
        }
        if (nextData == null || nextData.getClockInBean() == null) {
            return false;
        }
        return true;
    }

    @Override
    public int getItemCount() {
        return dataList == null ? 0 : dataList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_num;
        RelativeLayout root_view;
        //点击或者单个选中
        View onlySelectView;
        //已签到选中
        View selectedView;
        ImageView ivRemind;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_num = itemView.findViewById(R.id.tv_num);
            root_view = itemView.findViewById(R.id.root_view);
            onlySelectView = itemView.findViewById(R.id.only_select_view);
            selectedView = itemView.findViewById(R.id.selected_view);
            ivRemind = itemView.findViewById(R.id.iv_remind);
        }
    }

    public interface CalendarItemClickListener {
        void itemClick(CalendarData data, int position);
    }
}
